package com.hengpeng.msls.controller;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;

import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.itfinbase.message.ResponseMessage;
import com.hengpeng.msls.constants.LSConstants;
import com.hengpeng.msls.expresslogin.dto.LoginResultDto;
import com.hengpeng.msls.service.ServiceFactory;
import com.hengpeng.msls.vo.SessionMember;

public class BaseController
{
	protected Log logger = LogFactory.getLog(this.getClass());

	@Autowired
	public ServiceFactory serviceFactory;


	public void refreshSessionMember(HttpServletRequest request)
	{
//		SessionMember member = serviceFactory.getMemberService().refreshSessionMember(getBetCardNo(request));
//		request.getSession().setAttribute(Constants.CLIENT_SESSION_MEMBER, member);
	}

	public static SessionMember getSessionMember(HttpServletRequest request)
	{
		SessionMember sm = (SessionMember) request.getSession().getAttribute(LSConstants.CLIENT_SESSION_MEMBER);
		return sm;
	}

	public <T> ServiceResult<T> validateFailure(String message)
	{
		return validateFailure(Constant.TRANSACTION_RESPONSE_CODE_UNKOWN, message);
	}

	public <T> ServiceResult<T> validateFailure()
	{
		return validateFailure("数据没有通过验证");
	}

	public <T> ServiceResult<T> validateFailure(String code, String message)
	{
		ServiceResult<T> sr = new ServiceResult<T>();
		sr.setCode(code);
		sr.setMessage(message);
		sr.setIsSuccess(false);
		return sr;
	}

	public String returnErrorPage(ModelMap map)
	{
		return returnErrorPage(map, null);
	}

	public String returnErrorPage(ModelMap map, String message)
	{
		if (message != null)
		{
			map.put("message", message);
		}
		return "/worng_page.jsp";
	}

	public static boolean validateObjectsNull(Object... obj)
	{
		if (obj == null)
			return true;
		for (Object o : obj)
		{
			if (o == null)
			{
				return true;
			}
		}
		return false;
	}

	public static String getIpAddr(HttpServletRequest request)
	{
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		{
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public <T> ServiceResult<T> getServiceResult(ResponseMessage response, Class<T> clz)
	{
		ServiceResult<T> sr = new ServiceResult<T>();
		sr.setCode(response.getRespCode());
		sr.setMessage(response.getRespDesc());
		sr.setReturnObject(response.getReturnObject());
		if (response.getRespCode().equals(Constant.TRANSACTION_RESPONSE_CODE_SUCCESS))
		{
			sr.setIsSuccess(true);
		}
		else
		{
			sr.setIsSuccess(false);
		}
		return sr;
	}

	/**
	 * <pre>
	 * 是否通过第三方登陆验证
	 * </pre>
	 * @param request
	 * @return
	 */
	public LoginResultDto getExpressLoginResult(HttpServletRequest request)
	{
		SessionMember member = (SessionMember) request.getSession().getAttribute(LSConstants.CLIENT_SESSION_MEMBER);
		if (member != null)
		{
			return member.getExpressLogin();
		}
		return null;
	}

	public void setExpressLoginResult(HttpServletRequest request, LoginResultDto dto)
	{
		SessionMember member = (SessionMember) request.getSession().getAttribute(LSConstants.CLIENT_SESSION_MEMBER);
		if (member == null)
		{
			member = new SessionMember();
			request.getSession().setAttribute(LSConstants.CLIENT_SESSION_MEMBER, member);
		}
		member.setExpressLogin(dto);
	}

	public static String getBetCardNo(HttpServletRequest request)
	{
		SessionMember member = getSessionMember(request);
		return member == null ? null : member.getBetcardno();
		// return "100020061002";
	}

	public static boolean validateObjectsNullOrEmpty(Object... obj)
	{
		if (obj == null)
			return true;
		for (Object o : obj)
		{
			boolean isEmpty;
			if (o instanceof String)
			{
				isEmpty = o.toString().trim().equals("");
			}
			else if (o instanceof Collection<?>)
			{
				Collection<?> list = (Collection<?>) o;
				isEmpty = list.size() == 0;
			}
			else
			{
				isEmpty = (o == null);
			}
			if (isEmpty)
				return true;
		}
		return false;
	}

	public String getBasePath(HttpServletRequest request)
	{
		return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
				+ request.getContextPath() + "/";
	}
}
